package ui;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import each_class.User;
import ui.global.GraphicsUtil;

public class UserInfoUI extends AbstractUI {
	
	private Button edit;
	private Button save;
	private TextBar comfirPpassWord;
	private boolean isEdit;
	private TextBar userName;
	private TextBar account;
	private TextBar passWord;
	private Label exceptionView;
	private User user;
	private Image avatarImage;
	private Button editAvatar;
	private Composite avatar;
	private String path;

	public UserInfoUI (Composite parent) {
		super(parent);
		
		avatar = new Composite(this, SWT.NONE);
		avatar.setRegion(ui.global.GraphicsUtil.createRoundedRectangle(0, 0, 0, 0, 64));
		avatar.setBackground(ui.global.UIColor.PUREWHITE);
		avatar.setBackgroundImage(SWTResourceManager.getImage("src/image/no_picture_x128.png"));
		avatar.setBounds(541, 60, 128, 128);
		
		editAvatar = new Button(avatar, SWT.NONE) {
			
			@Override
			public void mouseUp(MouseEvent e) {
				if (isEdit) {
					FileDialog fileDialog = new FileDialog(getShell());
					fileDialog.setFilterNames(new String[] {"图片"});
					fileDialog.setFilterExtensions(new String[] {"*.jpg;*.jpeg;*.png"});
					fileDialog.setText("请选择头像");
					fileDialog.setFilterPath("SystemDrive");
					path = fileDialog.open();
					editAvatar.setVisible(isEdit);
					if (path != null) {
						UserInfoUI.this.avatarImage = new Image(null, path);
						avatar.setBackgroundImage(GraphicsUtil.resize(UserInfoUI.this.avatarImage, 128, 128));
					}
				}
			}
		};
		editAvatar.initial(
				SWTResourceManager.getImage("src/image/edit_deactive_x64.png"),
				SWTResourceManager.getImage("src/image/edit_active_x64.png"));
		editAvatar.setBounds(32, 32, 64, 64);
		editAvatar.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(SWTResourceManager.getImage("src/image/edit_active_x64.png"), 64, 64));
		editAvatar.setVisible(false);
		avatar.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if(e.x > 32 && e.y > 32 && e.x < 96 && e.y < 96 ) {
					return;
				}
				editAvatar.setVisible(true);
				GC gc = new GC(avatar);
				gc.setAlpha(100);
				gc.setBackground(ui.global.UIColor.BLACK);
				gc.setLineWidth(128);
				gc.drawLine(0, 64, 128, 64);
				gc.dispose();
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				if (e.x > 32 && e.y > 32 && e.x < 96 && e.y < 96 ) {
					return;
				}
				editAvatar.setVisible(false);
				avatar.redraw();
			}
		});
		
		account = new TextBar(this, SWT.NONE);
		account.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		account.setBounds(470, 260, 270, 40);
		account.setBackground(ui.global.UIColor.GRAY);
		account.setMessage("账号");
		account.setEditable(false);
		
		userName = new TextBar(this, SWT.NONE);
		userName.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		userName.setBounds(470, 320, 270, 40);
		userName.setBackground(ui.global.UIColor.GRAY);
		userName.setMessage("昵称");
		userName.setEditable(false);

		passWord = new TextBar(this, SWT.PASSWORD);
		passWord.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		passWord.setBounds(470, 380, 270, 40);
		passWord.setBackground(ui.global.UIColor.GRAY);
		passWord.setMessage("密码");
		passWord.setText("");
		passWord.setEditable(false);

		comfirPpassWord = new TextBar(this, SWT.PASSWORD);
		comfirPpassWord.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		comfirPpassWord.setBounds(470, 380, 270, 40);
		comfirPpassWord.setBackground(ui.global.UIColor.GRAY);
		comfirPpassWord.setMessage("确认密码");
		comfirPpassWord.setVisible(false);
		comfirPpassWord.setText("");
		comfirPpassWord.setEditable(false);
		

		save = new Button(this, SWT.NONE) {

			@Override
			public void mouseUp(MouseEvent e) {
				if (isEdit) {
					String userName = UserInfoUI.this.userName.getText();
					String password = UserInfoUI.this.passWord.getText();
					String comfirPpassWord = UserInfoUI.this.comfirPpassWord.getText();
					if (userName.equals("")) {
						showException("昵称不能为空");
					}
					if (comfirPpassWord.equals("") || password.equals("")
							|| !comfirPpassWord.equals(password)) {
						showException("密码不一致！");
						return;
					}
					new Thread() {
						public void run() {
							getDisplay().syncExec(new Runnable() {

								@Override
								public void run() {
									UserInfoUI.this.user = ui.global.Operation.updateUserInof(userName, password, path);
								}
							});
							if (UserInfoUI.this.user != null) {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										UserInfoUI.this.exceptionView.setText("修改成功！");
										UserInfoUI.this.exceptionView.setVisible(true);
										title.setText(user.getUname());
									}
								});
								try {
									Thread.sleep(1000L);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										ui.global.Current.getPlaySationButton().active();
									}
								});
								ui.global.Current.setCurrentUser(user);
							} else {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										showException("修改失败，请检查网络");
									}
								});
							}
						};
					}.start();
				}
			}
		};
//		save.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(
//				SWTResourceManager.getImage("src/image/save_deactive_x64.png"), 64, 64));
		save.initial(SWTResourceManager.getImage("src/image/save_deactive_x64.png"),
				SWTResourceManager.getImage("src/image/save_active_x64.png"));
		save.setBounds(641, 483, 64, 64);

		edit = new Button(this, SWT.NONE) {

			@Override
			public void mouseUp(MouseEvent e) {
				loadRegistPage();
				isEdit = true;
			}
		};

//		edit.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(
//				SWTResourceManager.getImage("src/image/edit_userInfo_deactive_x64.png"), 64, 64));
		edit.initial(
				SWTResourceManager.getImage("src/image/edit_userInfo_deactive_x64.png"),
				SWTResourceManager.getImage("src/image/edit_userInfo_active_x64.png"));
		edit.setBounds(501, 480, 64, 64);

		exceptionView = new Label(this, SWT.CENTER);
		exceptionView.setBounds(0, 604, 1210, 32);
		exceptionView.setForeground(ui.global.UIColor.RED);
		exceptionView.setVisible(false);
		
	}

	@Override
	public void active() {
		setVisible(true);
		initail();
	}

	private void initail(){
		isEdit = false;
		try {
			title.setText(ui.global.Current.getCurrentUser().getUname());
			new Thread() {
				public void run() {
					InputStream inputStream = ui.global.Operation.getUserAvatar(ui.global.Current.getCurrentUser());
					if (inputStream != null) {
						avatarImage = new Image(null, inputStream);
					}else {
						avatarImage = SWTResourceManager.getImage("src/image/no_picture_x128.png");
					}
					getDisplay().asyncExec(new Runnable() {
						
						@Override
						public void run() {
							avatar.setBackgroundImage(avatarImage);
						}
					});
				};
			}.start();
			account.setText(ui.global.Current.getCurrentUser().getUser_id());
			userName.setText(ui.global.Current.getCurrentUser().getUname());
			userName.setEditable(false);
			passWord.setText(ui.global.Current.getCurrentUser().getUpassword());
			passWord.setText("");
			passWord.setEditable(false);
			comfirPpassWord.setBounds(470, 380, 270, 40);
			comfirPpassWord.setText("");
			comfirPpassWord.setEditable(false);
			comfirPpassWord.setVisible(false);
			save.setBounds(641, 483, 64, 64);
			edit.setBounds(501, 480, 64, 64);
			edit.setVisible(true);
		} catch (NullPointerException e) {
		}
	}
	
	private void showException(String exception) {
		exceptionView.setText(exception);
		exceptionView.setVisible(true);
		new Thread() {

			public void run() {

				try {
					Thread.sleep(1000l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						exceptionView.setVisible(false);
					}
				});

			};
		}.start();
	}

	private void loadRegistPage() {
		userName.setEditable(true);
		passWord.setEditable(true);
		comfirPpassWord.setEditable(true);
		edit.setVisible(false);
		new Thread() {
			public void run() {
				for (int i = 0; i < 10; i++) {
					getDisplay().asyncExec(new Runnable() {

						@Override
						public void run() {
							save.setLocation(save.getLocation().x - 7, 483);
						}
					});
					try {
						Thread.sleep(20l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						comfirPpassWord.setVisible(true);
						comfirPpassWord.setEditable(true);
					}
				});

				for (int i = 0; i < 10; i++) {
					getDisplay().asyncExec(new Runnable() {

						@Override
						public void run() {
							save.setLocation(571, save.getLocation().y + 6);
							comfirPpassWord.setLocation(470, comfirPpassWord.getLocation().y + 6);
						}
					});
					try {
						Thread.sleep(20l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}
}
